package com.ssi.myshop.utils;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;


import com.ssi.myshop.R;

/**
 * Loading动画类
 */
public class LoadingUtils {
    private static final String TAG = "LoadingUtils";
    private static AlertDialog loadingDialog;

    public static void loading(Context context, String loadingText) {

        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View dialogView = layoutInflater.inflate(R.layout.loading_alert, null);
        TextView mTvLoading = dialogView.findViewById(R.id.tv_loading);
        if (!loadingText.equals("")){
            mTvLoading.setText(loadingText);
        }


        final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.alert_dialog);
        loadingDialog = builder.create();


        loadingDialog.show();
        loadingDialog.setCancelable(false);
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setContentView(dialogView);

        WindowManager.LayoutParams lp = loadingDialog.getWindow().getAttributes();
        lp.width = SizeUtils.dip2px(context, 200);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        loadingDialog.getWindow().setAttributes(lp);


    }

    public static void dismissLoadingDialog() {


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != loadingDialog && loadingDialog.isShowing()) {
                    try {
                        loadingDialog.cancel();
                    }catch (Exception e){
                    }
                }
            }
        }, 1000);
    }


}
